import turtle
import random

# 设置画布
screen = turtle.Screen()
screen.title("沙漠中的梭梭树")
screen.setup(width=800, height=600)
screen.bgcolor("#f5deb3")  # 浅棕色背景，代表沙漠天空

# 创建绘图 turtle
pen = turtle.Turtle()
pen.speed(3)  # 中等速度
pen.hideturtle()

def draw_sand():
    """绘制沙漠地面"""
    pen.penup()
    pen.goto(-400, -200)
    pen.pendown()
    pen.color("#e9c46a")  # 沙色
    pen.begin_fill()
    for _ in range(2):
        pen.forward(800)
        pen.right(90)
        pen.forward(300)
        pen.right(90)
    pen.end_fill()

def draw_sand_dunes():
    """绘制沙丘起伏"""
    pen.penup()
    pen.goto(-400, -200)
    pen.pendown()
    pen.color("#d4a373")  # 深一点的沙色
    pen.begin_fill()
    pen.setheading(0)
    pen.forward(100)
    pen.left(60)
    pen.forward(50)
    pen.right(120)
    pen.forward(120)
    pen.left(100)
    pen.forward(80)
    pen.right(140)
    pen.forward(150)
    pen.left(110)
    pen.forward(90)
    pen.right(130)
    pen.forward(200)
    pen.left(100)
    pen.forward(70)
    pen.right(120)
    pen.forward(180)
    pen.left(90)
    pen.forward(300)
    pen.left(90)
    pen.forward(800)
    pen.end_fill()

def draw_haloxylon(x, y, height):
    """绘制梭梭树"""
    pen.penup()
    pen.goto(x, y)
    pen.pendown()
    
    # 树干
    pen.color("#8b4513")
    pen.begin_fill()
    pen.setheading(90)
    pen.forward(height * 0.3)
    pen.right(90)
    pen.forward(height * 0.05)
    pen.right(90)
    pen.forward(height * 0.3)
    pen.right(90)
    pen.forward(height * 0.05)
    pen.end_fill()
    
    # 树枝和叶子（梭梭树呈灌木状）
    pen.penup()
    pen.goto(x, y + height * 0.3)
    pen.pendown()
    pen.color("#2e8b57")  # 绿色
    
    # 绘制多个分枝
    branches = 6
    for i in range(branches):
        angle = (i - branches/2) * 30  # 分支角度
        branch_length = height * 0.25
        pen.penup()
        pen.goto(x, y + height * 0.15 + i * (height * 0.3 / branches))
        pen.pendown()
        pen.setheading(90 + angle)
        pen.forward(branch_length)
        
        # 小分枝
        pen.setheading(90 + angle + 45)
        pen.forward(branch_length * 0.5)
        pen.backward(branch_length * 0.5)
        pen.setheading(90 + angle - 45)
        pen.forward(branch_length * 0.5)
        pen.backward(branch_length * 0.5)
        pen.backward(branch_length)

def draw_sun():
    """绘制太阳"""
    pen.penup()
    pen.goto(250, 200)
    pen.pendown()
    pen.color("#ffd700")
    pen.begin_fill()
    pen.circle(30)
    pen.end_fill()
    
    # 太阳光芒
    for _ in range(12):
        pen.penup()
        pen.goto(250, 230)
        pen.pendown()
        pen.setheading(pen.heading() + 30)
        pen.forward(20)
        pen.backward(20)

def draw_small_plants():
    """绘制沙漠中的小植物"""
    positions = [(-300, -150), (-100, -180), (200, -160), (300, -120)]
    for x, y in positions:
        pen.penup()
        pen.goto(x, y)
        pen.pendown()
        pen.color("#8b4513")  # 棕色茎
        pen.setheading(90)
        pen.forward(random.randint(10, 20))
        
        pen.color("#2e8b57")  # 绿色叶子
        pen.setheading(45)
        pen.forward(random.randint(5, 10))
        pen.backward(random.randint(5, 10))
        pen.setheading(135)
        pen.forward(random.randint(5, 10))

def draw_main():
    """主函数，绘制整个场景"""
    draw_sand()
    draw_sand_dunes()
    draw_sun()
    
    # 绘制几棵不同大小的梭梭树
    draw_haloxylon(-200, -150, 80)
    draw_haloxylon(0, -180, 100)
    draw_haloxylon(150, -160, 70)
    draw_haloxylon(-300, -120, 60)
    
    # 绘制小植物
    draw_small_plants()
    
    # 完成绘制
    turtle.done()

if __name__ == "__main__":
    draw_main()
